﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using intelliEssay.Core;
using intelliEssay.Core.EnvironmentVariables;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using intelliEssay.Core.DocumentStructure;
using intelliEssay.Interop.FontServices;

namespace intelliEssay.Checkers.FixedDocumentElementChecker
{

    public class FixedDocumentElementChecker : IEssayChecker
    {
        /// <summary>
        /// foreach paragraph that has "exact" attribute, do the check.
        /// </summary>
        /// <param name="task"></param>
        public void Run(Core.Task task)
        {
            var standardParagraphsWithExactTag = task.taskSetting.standardDocument.MainDocumentPart.Document.Body.Elements<Paragraph>().Where<Paragraph>(para =>
                {
                    return para.GetAttributes().Any<OpenXmlAttribute>(attr => attr.LocalName == "exact" && attr.NamespaceUri == intelliEssay.Core.EnvironmentVariables.Constants.intelliEssayUri);
                });
            Interop.FontServices.RunInfoHelper helper = new RunInfoHelper(task.TaskObjectRoot);
            foreach (Paragraph para in standardParagraphsWithExactTag)
            {
                var attr = para.GetAttribute(Constants.DocPartLocalName, Constants.intelliEssayUri);
                int Length = 0;
                DocumentParts part = (DocumentParts)Enum.Parse(typeof(DocumentParts), attr.Value, true);
                var index = task.documentStructure.GetDocumentKeyElementIndexByName(part, out Length);
                if (index != null)
                {
                    Paragraph p = task.documentStructure.AllParagraph[index.Value];
                    if (part != DocumentParts.KeywordInEnglish && part != DocumentParts.KeywordInChinese)
                    {
                        if (p.InnerText.Trim() != para.InnerText.Trim())
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, part.ToLocalizedString(-1) + "的写法应为" + para.InnerText, p, task, true));
                        }
                    }
                    else
                    {
                        if (p.InnerText.StartsWith(para.InnerText) == false)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, part.ToLocalizedString(-1) + "的写法错误" + para.InnerText, p, task, true));
                        }
                    }
                }
            }
        }

        public void Cancel(Core.Task task)
        {

        }


        public string GetDescription(string cultureInfo)
        {
            return "Checks texts for fixed parts of the document.";
        }

        public string GetName(string cultureInfo)
        {
            return "Document Fixed Parts Checker";
        }
    }
}
